package com.jdrx.sw.sewage.common.util;

import com.jdrx.sw.sewage.common.constant.Constant;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.Objects;

import static com.jdrx.sw.sewage.common.constant.Constant.*;

/**
 * Created by fdk on 2019/7/16.
 */
public class LocalDateTimeUtils {

    /**
     * yyyy-MM-dd HH:mm:ss格式转换成LocalDate
     * @param localDateTimeString
     * @return
     */
    public static LocalDate localDateTimeString2LocalDate(String localDateTimeString) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT);
        LocalDateTime localDateTime = null;
        try {
            localDateTime = LocalDateTime.parse(localDateTimeString,df);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return Objects.nonNull(localDateTime) ? localDateTime.toLocalDate() : LocalDate.now();
    }


    /**
     * LocalDate转换成yyyy-MM-dd格式字符串
     * @param localDate
     * @return
     */
    public static String localDate2String(LocalDate localDate) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(Constant.DEFAULT_DATE_FORMAT);
        return df.format(localDate);
    }

    /**
     * LocalDateTime转换成yyyy-MM-dd格式字符串
     * @param localDateTime
     * @return
     */
    public static String localDate2String(LocalDateTime localDateTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(Constant.DEFAULT_DATE_FORMAT);
        return df.format(localDateTime);
    }


    /**
     * LocalDateTime转换成yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param localDateTime
     * @return
     */
    public static String localDateTime2String(LocalDateTime localDateTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT);
        return df.format(localDateTime);
    }

    /**
     * LocalDateTime转换成yyyy-MM-dd HH:mm:ss格式字符串
     *
     * @param localDateTime
     * @return
     */
    public static String localDateTime3String(LocalDateTime localDateTime) {
        DateTimeFormatter df = DateTimeFormatter.ofPattern(YMD_TIME_FORMAT);
        return df.format(localDateTime);
    }

    /**
     * String按照格式转换成LocaldateTime
     * @param dateString
     * @return
     */
    public static LocalDateTime stringToLocalDateTime(String dateString) {
        DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMAT);
        return LocalDateTime.parse(dateString, timeDtf);
    }

    /**
     * String按照格式转换成Localdate
     *
     * @param dateString
     * @return
     */
    public static LocalDate stringToLocalDate(String dateString) {
        DateTimeFormatter timeDtf = DateTimeFormatter.ofPattern(Constant.DEFAULT_DATE_FORMAT);
        return LocalDate.parse(dateString, timeDtf);
    }

    /**
     * String按照格式转换成Localdate
     *
     * @param date
     * @return
     */
    public static LocalDateTime dataToLocalDate(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofEpochSecond(time / 1000, 0, ZoneOffset.ofHours(8));
        return dateTime;
    }

    /**
     * Localdate转成yyMMdd字符串
     *
     * @param
     * @return
     */
    public static String localDateToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YMD_TIME_FORMAT);
        return sdf.format(date);
    }

    /**
     * Localdate转成yyMMdd字符串
     *
     * @param
     * @return
     */
    public static String localDateToString2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(MD_TIME_FORMAT);
        return sdf.format(date);
    }

    /**
     * Localdate转成yyyy-MM字符串
     *
     * @param
     * @return
     */
    public static String localDateCURRMONTHToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YMD_TIME_FORMAT_CURRMONTH);
        return sdf.format(date);
    }

    /**
     * Localdate转成yyyy字符串
     *
     * @param
     * @return
     */
    public static String dateCURRToString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
        return sdf.format(date);
    }

    /**
     * Localdate转成yyyy字符串
     *
     * @param
     * @return
     */
    public static String localDateCURRYoString(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(YMD_TIME_FORMAT_CURRY);
        return sdf.format(date);
    }

    /**
     * Localdate转成yyyy字符串
     *
     * @param
     * @return
     */
    public static String dateCURRToString2(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_H);
        return sdf.format(date);
    }

    public static String dateCURRToString3(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_HM);
        return sdf.format(date);
    }

    /**
     * Localdate转成yyyy字符串
     *
     * @param
     * @return
     */
    public static String dateCURRToString4(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_FORMAT_DAY);
        return sdf.format(date);
    }

    /**
     * 根据传入的分钟转化为对应的天数小时分钟 如 : 1天12小时23分
     *
     * @param minute 分钟
     * @return
     */
    public static String getNumTime(long minute) {
        String days = "";
        if (minute < 60) {
            days = String.valueOf(minute) + "分钟";
        } else if (minute < 1440) {
            long value = minute % 60;
            days = String.valueOf((minute - value) / 60) + "小时"
                    + String.valueOf(value) + "分钟";
        } else {
            long MinuteValue = minute % 60;
            long value = (minute - MinuteValue) / 60;
            long hourValue = value % 24;
            long dayValue = (value - hourValue) / 24;
            days = String.valueOf(dayValue) + "天" + String.valueOf(hourValue) + "小时"
                    + MinuteValue + "分钟";
        }
        return days;
    }

    /**
     * 在时间上加几个月
     */
    public static Date stepMonth(Date sourceDate, int month) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.MONTH, month);

        return c.getTime();
    }

    /**
     * 在时间上加几个小时
     */
    public static Date addHour(Date sourceDate, int hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.HOUR, hour);
        return c.getTime();
    }

    /**
     * 在时间上减几个小时
     */
    public static Date stepHour(Date sourceDate, int hour) {
        Calendar c = Calendar.getInstance();
        c.setTime(sourceDate);
        c.add(Calendar.HOUR, -hour);

        return c.getTime();
    }

    /**
     * 在时间上价减几天
     */
    public static LocalDateTime stepOrAddDay(LocalDateTime time, int num) {
        if (num > 0) return time.plusMonths(0).plusDays(num).plusHours(0).plusMinutes(0).plusSeconds(0);
        if (num <= 0) return time.minusDays(-num);
        return null;
    }

    /**
     * 判断两个时间相差几个小时
     */
    public static Long timeDiff(String pBeginTime, String pEndTime) {
        SimpleDateFormat format = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
        Long beginL = null;
        Long endL = null;
        try {
            beginL = format.parse(pBeginTime).getTime();
            endL = format.parse(pEndTime).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
//        Long day = (endL - beginL)/86400000;
        Long hour = (endL - beginL) / 3600000;
//        Long min = ((endL - beginL)%86400000%3600000)/60000;
        return hour;
    }

    /**
     * 获取指定时间得年  2015-02-09 10:15:24
     */
    public static Integer getYear(Date date, String type) {
        //时间字符串转换格式
//        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
//        int hour = Integer.parseInt(String.format("%tH",date));
//        int minute = Integer.parseInt(String.format("%tM",date));
//        int second = Integer.parseInt(String.format("%tS",date));
        if ("Y".equals(type)) return Integer.parseInt(String.format("%tY", date));
        if ("M".equals(type)) return Integer.parseInt(String.format("%tm", date));
        if ("D".equals(type)) return Integer.parseInt(String.format("%td", date));
        return null;
    }

    /**
     * 获取当月的天
     */
    public static Integer getMothIsDay() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        return day;
    }

    /**
     * 在时间上减少小时
     */
    public static Date reduceHour(Integer time) {
        Date dt = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_DATE_TIME_FORMAT);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        //rightNow.add(Calendar.DATE, -1);
        rightNow.add(Calendar.HOUR, -time);
        Date dt1 = rightNow.getTime();

        return dt1;
    }

    /**
     * 在时间上减少小时
     */
    public static String getYesterday() {
        DateFormat dateFormat = new SimpleDateFormat("MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        String yesterdayDate = dateFormat.format(calendar.getTime());
        return yesterdayDate;
    }

    public static Boolean getCrrWeekZeroPoint() {
        Calendar cal = Calendar.getInstance();
        if (2 == cal.get(Calendar.DAY_OF_WEEK) && 00 == cal.getTime().getHours()) return true;
//        if (4 == cal.get(Calendar.DAY_OF_WEEK) && 14 == cal.getTime().getHours()) return true;
        return false;
    }

    public static void main(String[] args) {
/*        System.out.println(reduceHour(1));
        LinkedHashMap map = new LinkedHashMap();
        map.put("调节池液位", 1);
        map.put("进水瞬时流量", 1);
        map.put("进水累计流量", 1);
        map.put("出水瞬时流量", 1);
        map.put("出水累计流量", 1);
        map.put("出水氨氮", 1);
        map.put("出水COD", 1);
        map.put("出水TN", 1);
        map.put("出水TP", 1);

        JSONObject jsonObject = JSON.parseObject(JSONObject.toJSONString(map));

        System.out.println(jsonObject);
        Pattern pattern = Pattern.compile("[0-9]*");
        System.out.println(pattern.matcher("123").matches());
        */
//        String 兴盛 = String.format(STATION_OUTOFCONTACT_NETWORK_CH, "兴盛", "123456");
//        System.out.println(兴盛);
        /*String[] data = LocalDateTimeUtils.getYesterday().split("-");
        System.out.println(data[0]);
        System.out.println(data[1]);*/
        System.out.println(dataToLocalDate(new Date().getTime()));
    }
}
